// .........................................................................
// Title: fund_holdings_summary.do
//
// Constructs compact summary versions of the Morningstar fund holdings data
// .........................................................................

cap mkdir $tmp/fund_holdings
cap mkdir $tmp/morningstar_summary

* -------------------------------------------
* Compact panel of fund holdings
* -------------------------------------------

* Summary versions of quarterly files, split by quarter
forval year=2005/2020 {
    forval quarter_code=1/4 {
        use "$morningstar_hd/HD_`year'_q.dta", clear
        keep if date_q == tq(`year'q`quarter_code')
        gen marketvalue_usd = marketvalue / lcu_per_usd_eop
        keep cusip iso_country_code isin DomicileCountryId mns_class mns_subclass cgs_domicile marketvalue_usd
        gcollapse (sum) marketvalue_usd, by(cusip isin iso_country_code DomicileCountryId mns_class mns_subclass cgs_domicile)
        save "$tmp/fund_holdings/HD_`year'q`quarter_code'_summary", replace
    }
}

* Compact quarterly files
forval x = 1995/2020 {
    di "Processing `x'"
    use MasterPortfolioId cusip marketvalue lcu_per_usd_eop date_q using "$morningstar_hd/HD_`x'_q.dta", clear
    gen marketvalue_usd = marketvalue / lcu_per_usd_eop
    drop if missing(MasterPortfolioId) | missing(cusip) | missing(date_q)
    gcollapse (sum) marketvalue_usd, by(MasterPortfolioId cusip date_q)
    save "$tmp/fund_holdings/HD_`x'_q_compact", replace
}

* Append them
clear
forval x = 1995/2020 {
    di "Appending `x'"
    append using "$tmp/fund_holdings/HD_`x'_q_compact"
}
compress
save "$tmp/fund_holdings/HD_all_q_compact", replace

* Fields from security field
use cusip issuance_date using "$raw/cmns/gcap_security_master_cusip.dta", clear
save "$tmp/secmaster_issuance_date", replace
use cusip class_code1 class_code2 using "$raw/cmns/gcap_security_master_cusip.dta", clear
save "$tmp/secmaster_class_codes", replace

* Equities compact panel
use "$tmp/fund_holdings/HD_all_q_compact", clear
mmerge cusip using "$tmp/secmaster_class_codes", unmatched(m)
keep if class_code1 == "E"
drop _merge class_code1
drop if missing(marketvalue_usd)
drop if marketvalue_usd == 0
save "$tmp/fund_holdings/HD_all_q_compact_E", replace

* BC compact panel
use "$tmp/fund_holdings/HD_all_q_compact", clear
mmerge cusip using "$tmp/secmaster_class_codes", unmatched(m)
keep if class_code2 == "BC"
drop _merge class_code1 class_code2
drop if missing(marketvalue_usd)
drop if marketvalue_usd == 0
save "$tmp/fund_holdings/HD_all_q_compact_BC", replace

* --------------------------------------
* Security-level summary files
* --------------------------------------

* Quarterly summaries at security level
forval year = 2005/2020 {
    
    di "Processing `year'"
    use cusip marketvalue lcu_per_usd currency_id DomicileCountryId mns_class mns_subclass fundtype_mstar date_q ///
        using "$morningstar_hd/HD_`year'_q", clear
    cap drop cusip6 cgs_domicile
    gen issuer_number = substr(cusip, 1, 6)
    gen marketvalue_usd = marketvalue / lcu_per_usd
    collapse (sum) marketvalue_usd (firstnm) currency_id, by(DomicileCountryId fundtype_mstar cusip date_q mns_class mns_subclass issuer_number)
    
    drop if cusip == "#N/A N/A" | missing(cusip)
    gen asset_class = "Equity" if mns_class == "E"
    replace asset_class = "Bonds - Corporate" if mns_class == "B" & ~inlist(mns_subclass, "S", "A", "LS", "SF", "SV")
    replace asset_class = "Bonds - Government" if mns_class == "B" & inlist(mns_subclass, "S", "A", "LS")
    replace asset_class = "Bonds - Structured Finance" if mns_class == "B" & inlist(mns_subclass, "SF")
    replace asset_class = "Bonds - Sovranational" if mns_class == "B" & inlist(mns_subclass, "SV")
    drop if missing(asset_class)
    replace DomicileCountryId = "EMU" if inlist(DomicileCountryId, $eu1)
    replace DomicileCountryId = "EMU" if inlist(DomicileCountryId, $eu2)
    replace DomicileCountryId = "EMU" if inlist(DomicileCountryId, $eu3)
    collapse (sum) marketvalue_usd (firstnm) currency_id, by(DomicileCountryId fundtype_mstar cusip date_q asset_class issuer_number)
    
    mmerge issuer_number using "$raw/cmns/cmns_aggregation", unmatched(m)
    cap drop _merge 
    drop *source*
    format %35s *name*
    order marketvalue_usd, last
    save "$tmp/morningstar_summary/mns_sec_summary_`year'_q", replace

}

* Append security summaries
clear
gen year = .
forval year = 2005/2020 {
    append using "$tmp/morningstar_summary/mns_sec_summary_`year'_q"
    replace year = `year' if missing(year)
}
gsort year DomicileCountryId cusip
drop if marketvalue_usd == 0
compress
keep if inlist(DomicileCountryId, "USA", "EMU", "GBR", "CAN", "CHE", "AUS") | inlist(DomicileCountryId, "SWE", "DNK", "NOR")
mmerge cusip using "$raw/cmns/gcap_security_master_cusip.dta", unmatched(m) uname(f_)
replace asset_class = "Bonds - Structured Finance" if asset_class == "Bonds - Corporate" & f_class_code2 == "BSF"
drop f_* _merge
save "$tmp/morningstar_summary/mns_security_summary_q", replace

* Collapse to issuer level
use "$tmp/morningstar_summary/mns_security_summary_q", clear
gcollapse (sum) marketvalue_usd, by(year DomicileCountryId issuer_number asset_class issuer_name cgs_domicile cusip6_up_bg country_bg issuer_name_up)
save "$tmp/morningstar_summary/mns_issuer_summary_q", replace

* Collapse to cusip-quarter level
use "$tmp/morningstar_summary/mns_security_summary_q", clear
gcollapse (sum) marketvalue_usd, by(cusip date_q)
save "$tmp/morningstar_summary/mns_cusip_quarter_amounts", replace

* Collapse to cusip-domicile-quarter level
use "$tmp/morningstar_summary/mns_security_summary_q", clear
gcollapse (sum) marketvalue_usd, by(cusip DomicileCountryId date_q)
save "$tmp/morningstar_summary/mns_cusip_domicile_quarter_amounts", replace
